/*
 * @Author: gyc
 * @Date: 2020-05-26 14:56:35
 * @LastEditTime: 2020-08-26 16:31:30
 * @Description: Nuxt测试版
 */
const Service = require('egg').Service;
const moment = require('moment');
const AES = require("../../config/utils")
class UserService extends Service {
	//查找所有用户
	async findAll() {
		const user = await this.app.mysql.query("select * from yui_user")
		return user;
	}
	//登录用户匹配
	async findOne(username, password) {
		const user = await this.app.mysql.get("yui_user", {
			username: username,
			password: AES.Encrypt(password)
		})
		return user;
	}
	//寻找用户信息
	async findOneId(username) {
		const user = await this.app.mysql.get("yui_user", {
			username: username,
		})
		return user.id;
	}
	//寻找用户信息
	async findOneInfo(username) {
		const user = await this.app.mysql.get("yui_user", {
			username: username,
		})
		return user;
	}
	//注册用户
	async createUser(username, password) {
		const hasUser = await this.app.mysql.get("yui_user", {
			username: username
		})
		if (!!hasUser) {
			return {
				msg: "用户名已存在！",
				hasUser
			}
		} else {
			const updatetimes = moment().format('YYYY-MM-DD hh-mm-ss')
			const user = await this.app.mysql.insert("yui_user", {
				username: username,
				password: password,
				userstus: 1,
				administrator: "0",
				createAt: updatetimes,
				updateAt: updatetimes
			})
			return {
				msg: "创建用户成功！",
				hasUser
			};
		}
	}
	//查询用户信息
	async findUserinfo(userid) {
		const user = await this.app.mysql.get("yui_user_info", {
			userId: userid
		})
		return user;
	}
	//更新用户信息
	async updateUserinfo(userid, data) {
		const userInfo = await this.app.mysql.get("yui_user_info", {
			userId: userid
		})
		let createtimes, updatetimes, user;
		//判断是否第一次更新
		if (!!userInfo) {
			updatetimes = moment().format('YYYY-MM-DD hh-mm-ss')
			createtimes = userInfo.createTm
			const row = {
				nickName: data.nickName,
				sex: data.sex,
				hobby: data.hobby,
				bestStory: data.bestStory,
				QQ: data.QQ,
				signature: data.signature,
				createTm: createtimes,
				updateTm: updatetimes
			}
			const options = {
				where: {
					userId: userid
				}
			};
			user = await this.app.mysql.update("yui_user_info", row, options)
		} else {
			updatetimes = moment().format('YYYY-MM-DD hh-mm-ss')
			createtimes = moment().format('YYYY-MM-DD hh-mm-ss')
			const row = {
				userId: userid,
				nickName: data.nickName,
				sex: data.sex,
				hobby: data.hobby,
				bestStory: data.bestStory,
				QQ: data.QQ,
				signature: data.signature,
				createTm: createtimes,
				updateTm: updatetimes
			}
			user = await this.app.mysql.insert("yui_user_info", row)
		}
		return user;
	}
	//注册用户数量
	async userCount() {
		// const sql = "select * from yui_music where id=" + this.app.mysql.escape(1)
		const sql = "select count(id) from yui_music"
		const count = await this.app.mysql.query(sql)
		return count;

	}
	//用户密码修改
	async userReword(userid, oldPwd, newPwd) {
		let result
		const oPassword = await this.app.mysql.select("yui_user", {
			where: {
				id: userid
			},
			columns: ['password']
		})
		if (oPassword[0].password === oldPwd) {
			const row = {
				password: newPwd
			}
			const options = {
				where: {
					id: userid
				}
			}
			result = {
				code: "200",
				data: await this.app.mysql.update("yui_user", row, options)
			}
		} else {
			result = {
				code: "201",
				data: '旧密码错误!'
			}
		}
		return result;
	}
	//用户状态管理
	async userStusCtl(userid, stus) {
		const row = {
			userStus: stus
		}
		const options = {
			where: {
				id: userid
			}
		}
		const result = await this.app.mysql.update("yui_user", row, options)
		return result
	}
}

module.exports = UserService;